package com.chen.integral.module.integral.controller;

import com.chen.base.module.base.util.JWTInfo;
import com.chen.base.module.base.util.StringUtils;
import com.chen.base.module.base.dto.PageResponse;
import com.chen.base.module.base.dto.Response;
import com.chen.integral.module.integral.service.IntegralHistoryService;
import com.chen.integral.module.integral.vo.IntegerHistoryAddVo;
import com.chen.integral.module.integral.vo.IntegerHistoryOneVo;
import com.chen.integral.module.integral.vo.IntegerHistoryQueryVo;
import com.chen.integral.module.integral.vo.IntegerHistoryRemoveVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @Title: manager-spring-mvc-hb
 * @Description: to do
 * @Author: chenjiwei
 * @Date: 2020-11-16 22:33
 * @Version: 1.0.0
 */
@RestController
@RequestMapping("/integral/integraHistory")
public class IntegralHistoryController {
    private Logger log = LoggerFactory.getLogger(IntegralHistoryController.class);

    @Autowired
    private IntegralHistoryService integralHistoryService;

    @PostMapping("/query")
    public PageResponse queryIntegral(@RequestBody IntegerHistoryQueryVo vo){
        try{
            String username = JWTInfo.get().getUsername();
            System.out.println(username);
            return integralHistoryService.query(vo);
        }catch (Exception e){
            log.info(e.getMessage());
            return PageResponse.fail("查询失败");
        }
    }

    @PostMapping("/queryByIdIn")
    public PageResponse queryIntegralByIdIn(@RequestBody IntegerHistoryOneVo vo){
        try{

            return integralHistoryService.queryByIdIn(vo);
        }catch (Exception e){
            log.info(e.getMessage());
            return PageResponse.fail("查询失败");
        }
    }

    @PostMapping({"/save","/update"})
    public Response save(@RequestBody IntegerHistoryAddVo vo){
        try{
            if(StringUtils.isNotNull(vo.getId())){

                return Response.success("编辑成功");
            }

            return Response.success("新增成功");
        }catch (Exception e){
            log.info(e.getMessage());
            return Response.fail("操作失败");
        }
    }

    @PostMapping("/remove")
    public Response remove(@RequestBody IntegerHistoryRemoveVo vo){

        try {

            return Response.success("删除成功");
        }catch (Exception e){
            log.info(e.getMessage());
            return Response.fail("删除失败");
        }
    }

    @PostMapping("/calcu")
    public Response calcu(){
        try {
            integralHistoryService.calcuIntegralByRule("login", "chenjiwei", new Date());
            return Response.success("成功");
        }catch (Exception e){
            e.printStackTrace();


            return Response.fail(e.getMessage());
        }
    }
}
